package com.example.backstage.config.security;

import com.example.backstage.entity.User;
import com.example.backstage.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

import java.util.Optional;

@Component
public class UserDetailsServiceImpl implements UserDetailsService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

        if (!username.isBlank()){
            Optional<User> user = userMapper.selectUserByName(username);
            if (user.isPresent()) {
                return new LoginUserDetails(user.get());
            }
        }
        return null;
    }
}
